package com.jiang.books.domain;

import static javax.persistence.GenerationType.SEQUENCE;

import java.io.Serializable;
import java.math.BigInteger;
import java.util.Date;

import javax.persistence.*;

import org.hibernate.annotations.Type;
import org.joda.time.DateTime;

/*
create table jiang_user_books 
    (
	    id                  Number(20,0) NOT NULL ENABLE,
		user_id             Number(20,0) NOT NULL ENABLE,
		book_id             Number(20,0) NOT NULL ENABLE,
		mark                Number(2,0),
		add_date            Date,
		user_comment        Varchar2(1000), 
		--audit
        created_by             Varchar2(20),
        created_date           TIMESTAMP,
        last_midified_by       Varchar2(20),
        last_modified_date     TIMESTAMP,
		CONSTRAINT pk_jiang_user_books PRIMARY KEY (user_id, book_id),
		CONSTRAINT fk_jiang_user_books_1 FOREIGN KEY (user_id) REFERENCES jiang_user (id) ON DELETE CASCADE ENABLE,
		CONSTRAINT fk_jiang_user_books_2 FOREIGN KEY (book_id) REFERENCES jiang_book (id) ON DELETE CASCADE ENABLE,
		CONSTRAINT ck_mark CHECK (mark in (1, 2, 3))
	);
*/

@Entity
@Table(name = "jiang_user_books")
@NamedQueries({
	@NamedQuery(name="UserBook.findAll",
			    query="select ub from UserBook ub"),
})
@SqlResultSetMapping(
		name="userBookResult",
		entities=@EntityResult(entityClass=UserBook.class)
)
public class UserBook implements Serializable {

	private static final long serialVersionUID = 1L;
	
	private BigInteger id;
    private User user;
    private Book book;
    private int mark;
	private Date addDate;
    private String userComment;
    private int version;
    
	// Audit fields
	private String createdBy;
	private DateTime createdDate;	
	private String lastModifiedBy;
	private DateTime lastModifiedDate;

    //private Set<Award> awards = new HashSet<Award>();

    public UserBook() {
	}
    
	public UserBook(User user, Book book) {
		this.user = user;
		this.book = book;
	}
    
	@Id
    @GeneratedValue(strategy = SEQUENCE, generator="JIANG_SEQ")
    @SequenceGenerator(name="JIANG_SEQ", sequenceName="jiang_id_sequence", allocationSize=350)
	@Column(name = "ID", unique = true, nullable = false)
	public BigInteger getId() {
		return this.id;
	}

	public void setId(BigInteger id) {
		this.id = id;
	}

    @ManyToOne
    @JoinColumn(name = "USER_ID")
    public User getUser() {
        return this.user;
    }

    public void setUser(User user) {
        this.user = user;
    }
    
    @ManyToOne
    @JoinColumn(name = "BOOK_ID")
    public Book getBook() {
        return this.book;
    }

    public void setBook(Book book) {
        this.book = book;
    }

    @JoinColumn(name = "MARK")
    public int getMark() {
        return this.mark;
    }

    public void setMark(int mark) {
        this.mark = mark;
    }
	
	@Temporal(TemporalType.DATE)
	@Column(name = "ADD_DATE")
	public Date getAddDate() {
		return this.addDate;
	}

	public void setAddDate(Date addDate) {
		this.addDate = addDate;
	}
	
    @Column(name = "USER_COMMENT")
    public String getUserComment() {
        return this.userComment;
    }

    public void setUserComment(String userComment) {
        this.userComment = userComment;
    }
    
    @Version
    @Column(name = "VERSION")
    public int getVersion() {
        return this.version;
    }

    public void setVersion(int version) {
        this.version = version;
    }
    
    
    // Audit methods
    @Column(name="CREATED_BY")
	public String getCreatedBy() {
		return createdBy;
	}

	public void setCreatedBy(String createdBy) {
		this.createdBy = createdBy;
	}

	@Column(name="CREATED_DATE")
	@Type(type="org.joda.time.contrib.hibernate.PersistentDateTime")
	public DateTime getCreatedDate() {
		return createdDate;
	}

	public void setCreatedDate(DateTime createdDate) {
		this.createdDate = createdDate;
	}

	@Column(name="LAST_MODIFIED_BY")
	public String getLastModifiedBy() {
		return lastModifiedBy;
	}
	
	public void setLastModifiedBy(String lastModifiedBy) {
		this.lastModifiedBy = lastModifiedBy;
	}

	@Column(name="LAST_MODIFIED_DATE")
	@Type(type="org.joda.time.contrib.hibernate.PersistentDateTime")
	public DateTime getLastModifiedDate() {
		return lastModifiedDate;
	}

	public void setLastModifiedDate(DateTime lastModifiedDate) {
		this.lastModifiedDate = lastModifiedDate;
	}
    
	
	@Transient
	public boolean isNew() {
		if (id == null) {
			return true;
		} else {
			return false;
		}
	}


	public String toString() {
		return "UserBook - Id: " + getId() + ", User: " + getUser()
				+ ", Book: " + getBook();
	}
}